package com.mengshu.platform.web.handler;

import com.mengshu.platform.web.resp.R;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * mvc统一格式返回
 */
@RestControllerAdvice(annotations = { RestController.class, ResponseBody.class })
public class ResponseFormatHandler implements ResponseBodyAdvice {

    @Override
    public boolean supports(MethodParameter methodParameter, Class clazz)  {
        return !R.class.isAssignableFrom(methodParameter.getParameterType());
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType
            , Class clazz, ServerHttpRequest request, ServerHttpResponse response) {
        return R.ok(o);
    }
}
